<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="需求将一个视图对象绕设定点旋转 旋转当我们需要将一个 View 旋转90°时，可以这样做   1self.view.transform &#x3D; CGAffineTransformMakeRotation(M_PI_2); 这时，这个 view 就会绕着“旋转中心”旋转，那么这个“旋转中心”能否自定义呢？肯定是可以的，继续往下看。。。   我们知道每一个 UIView 内部都关联着一个 CALayer">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS开发之 anchorPoint">
<meta property="og:url" content="https://vhuichen.github.io/2018/07/2018/07/iOS%E5%BC%80%E5%8F%91%E4%B9%8B%20anchorPoint/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="需求将一个视图对象绕设定点旋转 旋转当我们需要将一个 View 旋转90°时，可以这样做   1self.view.transform &#x3D; CGAffineTransformMakeRotation(M_PI_2); 这时，这个 view 就会绕着“旋转中心”旋转，那么这个“旋转中心”能否自定义呢？肯定是可以的，继续往下看。。。   我们知道每一个 UIView 内部都关联着一个 CALayer">
<meta property="og:locale">
<meta property="article:published_time" content="2018-07-17T16:00:00.000Z">
<meta property="article:modified_time" content="2018-08-12T02:07:52.407Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="anchorPoint">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://vhuichen.github.io/2018/07/2018/07/iOS%E5%BC%80%E5%8F%91%E4%B9%8B%20anchorPoint/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2018/07/2018/07/iOS%E5%BC%80%E5%8F%91%E4%B9%8B%20anchorPoint/","path":"2018/07/2018/07/iOS开发之 anchorPoint/","title":"iOS开发之 anchorPoint"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS开发之 anchorPoint | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9C%80%E6%B1%82"><span class="nav-number">1.</span> <span class="nav-text">需求</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%8B%E8%BD%AC"><span class="nav-number">1.1.</span> <span class="nav-text">旋转</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#anchorPoint-amp-position-amp-frame"><span class="nav-number">1.2.</span> <span class="nav-text">anchorPoint &amp; position &amp; frame</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2018/07/2018/07/iOS%E5%BC%80%E5%8F%91%E4%B9%8B%20anchorPoint/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS开发之 anchorPoint | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS开发之 anchorPoint
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2018-07-18 00:00:00" itemprop="dateCreated datePublished" datetime="2018-07-18T00:00:00+08:00">2018-07-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2018-08-12 10:07:52" itemprop="dateModified" datetime="2018-08-12T10:07:52+08:00">2018-08-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">iOS开发</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><p>将一个视图对象绕设定点旋转</p>
<h3 id="旋转"><a href="#旋转" class="headerlink" title="旋转"></a>旋转</h3><p>当我们需要将一个 View 旋转90°时，可以这样做  </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">self</span>.view.transform = <span class="built_in">CGAffineTransformMakeRotation</span>(M_PI_2);</span><br></pre></td></tr></table></figure>
<p>这时，这个 view 就会绕着“旋转中心”旋转，那么这个“旋转中心”能否自定义呢？肯定是可以的，继续往下看。。。  </p>
<p>我们知道每一个 UIView 内部都关联着一个 CALayer ，而 CALayer 中有一个属性</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Defines the anchor point of the layer&#x27;s bounds rect, as a point in</span></span><br><span class="line"><span class="comment"> * normalized layer coordinates - &#x27;(0, 0)&#x27; is the bottom left corner of</span></span><br><span class="line"><span class="comment"> * the bounds rect, &#x27;(1, 1)&#x27; is the top right corner. Defaults to</span></span><br><span class="line"><span class="comment"> * &#x27;(0.5, 0.5)&#x27;, i.e. the center of the bounds rect. Animatable. */</span></span><br><span class="line"><span class="keyword">@property</span> <span class="built_in">CGPoint</span> anchorPoint;</span><br></pre></td></tr></table></figure>
<p>这个 anchorPoint 俗称“锚点”，其实也就是上面所说的“旋转中心”。<br>从注释中我们可以看懂，anchorPoint 是一个已经“归一化”的数据，也就是默认值是中心（0.5, 0.5），左上角是（0, 0），右下角是（1, 1）。anchorPoint 是一个以自身为参考系的“归一化”坐标。<br>所以当我们想绕某个点旋转时，只需要将这个点相对于自身的“归一化”坐标赋值给 anchorPoint 就行了。但，事实上当我们改动 anchorPoint 时，发现 layer 也改动了。也就是 anchorPoint 和 frame 之间存在某种关系。</p>
<span id="more"></span>

<h3 id="anchorPoint-amp-position-amp-frame"><a href="#anchorPoint-amp-position-amp-frame" class="headerlink" title="anchorPoint &amp; position &amp; frame"></a>anchorPoint &amp; position &amp; frame</h3><p>默认情况下 position 和 anchorPoint 是同一个点，但 position 是相对于父类的，而 anchorPoint 是相对于自身的。anchorPoint 和 position 的改动互不影响，但会影响 frame 。它们之间存在这样的关系</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">frame.origin.x = position.x - anchorPoint.x * bounds.size.width；  </span><br><span class="line">frame.origin.y = position.y - anchorPoint.y * bounds.size.height；</span><br></pre></td></tr></table></figure>
<p>所以当我们改变 anchorPoint 或 position 的时候 layer 也会跟着改变（frame变了）。那么问题来了，我改变 anchorPoint 仅仅是为了后面的旋转，并没有想过要改变其 frame 。怎嘛办？ 可以这样解决  </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">CGRect</span> oldFrame = <span class="keyword">self</span>.label.frame;</span><br><span class="line"><span class="keyword">self</span>.label.layer.anchorPoint = <span class="built_in">CGPointMake</span>(<span class="number">0.5</span>, ((w / <span class="number">2</span> + <span class="number">5</span>) / <span class="keyword">self</span>.label.frame.size.height) + <span class="number">0.5</span>);<span class="comment">// 自定义</span></span><br><span class="line"><span class="keyword">self</span>.label.frame = oldFrame;</span><br><span class="line"><span class="keyword">self</span>.label.transform = <span class="built_in">CGAffineTransformMakeRotation</span>(M_PI_2);</span><br></pre></td></tr></table></figure>
<p>这样做在改变 anchorPoint 时就不会改变 frame 的值了。也就达到了我们一开始的需求。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/anchorPoint/" rel="tag"># anchorPoint</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2018/07/2018/07/UITextField%20%E6%B7%BB%E5%8A%A0%E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6/" rel="prev" title="UITextField 添加点击事件">
                  <i class="fa fa-chevron-left"></i> UITextField 添加点击事件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2018/08/2018/08/iOS%20%E6%95%B0%E6%8D%AE%E5%8E%BB%E9%87%8D%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E6%B3%95/" rel="next" title="iOS 数据去重的几种方法">
                  iOS 数据去重的几种方法 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
